package cn.tedu.baking.service.impl;

import cn.tedu.baking.commons.response.StatusCode;
import cn.tedu.baking.exception.ServiceException;
import cn.tedu.baking.mapper.UserMapper;
import cn.tedu.baking.pojo.dto.UserLoginDTO;
import cn.tedu.baking.pojo.dto.UserRegDTO;
import cn.tedu.baking.pojo.entity.User;
import cn.tedu.baking.pojo.vo.UserVO;
import cn.tedu.baking.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService implements IUserService {

    @Autowired
    UserMapper mapper;

    @Override
    public void reg(UserRegDTO userRegDTO) {

        UserVO userVO = mapper.selectByUsername(userRegDTO.getUsername());
        if(userVO != null){
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreateTime(new Date());
        mapper.insert(user);
    }

    @Override
    public UserVO login(UserLoginDTO userLoginDto) {
       UserVO userVO = mapper.selectByUsername(userLoginDto.getUsername());
       if(userVO==null){
           throw new ServiceException(StatusCode.USERNAME_ERROR);
       }
       if(!userVO.getPassword().equals(userLoginDto.getPassword())){
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
       return userVO;

    }
}
